package com.gwnet.rider.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gwnet.rider.service.PlatformWalletLogService;
import com.gwnet.rider.service.RiderWalletService;
import com.gwnet.shop.domain.OrderInfo;
import com.gwnet.shop.enums.OrderStatusEnum;
import com.gwnet.shop.service.IOrderDeliveryService;
import com.gwnet.shop.service.IOrderInfoService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 改变骑手钱包定时任务
 * @author: WangLiang
 * @date: 2024/8/6 15:02
 */
@Slf4j
@Component
@AllArgsConstructor
public class RiderWalletTask {
    /**
     * 最大确认收货退款时间7天
     */
    public static final int MAX_FINALLY_REFUND_TIME = 1;
    /**
     * 退款最长申请时间，当申请时间过了这个时间段之后，会取消退款申请
     */
    public static final int MAX_REFUND_APPLY_TIME = 1;
    public static final int DISTRIBUTION_SETTLEMENT_TIME = MAX_FINALLY_REFUND_TIME + MAX_REFUND_APPLY_TIME + 1;



    private final PlatformWalletLogService walletLogService;
    private final RiderWalletService riderWalletService;
    private final IOrderInfoService orderInfoService;
    private final IOrderDeliveryService deliveryService;


    /**
     * 订单完成后的 暂定 3 天执行订单结算
     */
//    @XxlJob("orderCommissionSettlement")
    public void orderCommissionSettlement(){
        log.info("开始执行订单结算任务》》》》》》》》》》》》》》》》》》》》》");
        // 查询出需要结算的订单信息
        Date now = new Date();
        LambdaQueryWrapper<OrderInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderInfo::getOrderStatus,OrderStatusEnum.SUCCESS.code())
                .lt(OrderInfo::getCompleteTime,now)
                .and(wrapper -> wrapper.eq(OrderInfo::getIsSettled,0).or().isNull(OrderInfo::getIsSettled));
        List<OrderInfo> list = orderInfoService.list(queryWrapper);

        // 执行结算操作
        orderInfoService.orderCommissionSettlement(list);
        log.info("结束执行订单结算任务》》》》》》》》》》》》》》》》》》》》》");

    }




}
